<?php
Globals::requireClass('Controller');
Globals::requireClass('Alipay');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Goods');
Globals::requireTable('Ordergoods');
Globals::requireTable('Vmoney');
Globals::requireTable('Vlevel');

class RechargeController extends Controller
{
    protected $UserTable;
	protected $GoodsTable;
	protected $OrderGoodsTable;
	protected $VmoneyTable;
	protected $VlevelTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->UserTable = new UserTable($config);
		$this->GoodsTable = new GoodsTable($config);
		$this->OrderGoodsTable = new OrdergoodsTable($config);
		$this->VmoneyTable = new VmoneyTable($config);
		$this->VlevelTable = new VlevelTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	//激活商品列表
	public function memberrechargeAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','list'=>array()));
	        exit();
	    }
	    
	    $list = array();
	    $data = $this->GoodsTable->listAll(array('status'=>1));
	    foreach ($data as $k=>$v){
	        $tmp['id'] = $v['id'];
	        $tmp['title'] = $v['title'];
	        $tmp['price'] = $v['price'];
	        $tmp['repertory'] = $v['repertory'];
	        $list[] = $tmp;
	    }
	    
	    echo json_encode(array('returncode'=>'0','list'=>$list));
	    exit();
	}
	
	//激活商品下单
	public function buyrechargeAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    $user = $this->UserTable->getRow($uid);
	    if($uid < 1 || $user['id'] < 1){
	        echo json_encode(array('returncode'=>-1,'orderid'=>'','price'=>'','title'=>''));
	        exit();
	    }
	    
	    $id = $this->getIntParam('id');
	    $goods = $this->GoodsTable->getRow($id);
	    if($goods['id'] < 1){
	        echo json_encode(array('returncode'=>-1,'orderid'=>'','price'=>'','title'=>''));
	        exit();
	    }
	    
	    $time = time();
	    $date = date('Y-m-d H:i:s');
	    
	    $data['orderid'] = substr($time, -9) . mt_rand(10, 99);
	    $data['uid'] = $uid;
	    $data['createtime'] = $date;
	    $data['status'] = 1;
	    $data['money'] = $goods['price'];
	    $data['goods_id'] = $goods['id'];
	    $this->OrderGoodsTable->add($data);
	    echo json_encode(array('returncode'=>0,'orderid'=>(string)$data['orderid'],'price'=>$goods['price'],'title'=>$goods['title']));
	    exit();
	}
	
	//激活商品支付成功
	public function buyrechargesucceedAction(){
	    $orderid = $this->getParam('orderid');
	    $payorderid = $this->getParam('payorderid');
	    $paytype = $this->getParam('paytype');
	    $sign = $this->getParam('ordersign');
	    $time = time();
	    
	    $order = $this->OrderGoodsTable->getRow(array('orderid'=>$orderid));
	    $goods = $this->GoodsTable->getRow($order['goods_id']);
	    $user = $this->UserTable->getRow($order['uid']);
	    
	    if($user['id'] < 1 || $goods['id'] <1 || $order['id'] < 1 || $order['status'] != 1){
	        echo json_encode(array('returncode'=>-1,'message'=>''));
	        exit();
	    }
	    /*
	    $new_num_arr = array();
	    $orderid_num = $orderid + 19921206;
	    for ($ii = 0 ; $ii < strlen($orderid_num) ; $ii++){
	        $tmp_num = substr($orderid_num, $ii , 1);
	        $new_num_arr[] = $tmp_num;
	    }
	    krsort($new_num_arr);
	    $new_num = '';
	    foreach ($new_num_arr as $v){
	        $new_num .= (string)$v;
	    }
	    if($sign != $new_num){
	        echo json_encode(array('returncode'=>-2,'message'=>''));
	        exit();
	    }
	    */
	    $paytypevalue = 0;
	    $paytype = empty($paytype) ? 'alipay' : $paytype;
	    if($paytype == 'alipay'){
	        $alipay = new Alipay();
	        $re = $alipay->queryone('',$payorderid);
	        $alipay_money = $re->response->trade->total_fee;
	        if($order['money'] != $alipay_money){
	            echo json_encode(array('returncode'=>-999,'message'=>''));
	            exit();
	        }
	        
	        $paytypevalue = 1;
	    }elseif($paytype == 'vmoney'){
	        $vcount = 0;
	        $listvalue = $this->VmoneyTable->listAllWithFields('money',array('uid'=>$user['id']));
	        foreach ($listvalue as $k=>$v){
	            $vcount += $v['money'];
	        }
	        if($vcount < $order['money']){
	            echo json_encode(array('returncode'=>-998,'message'=>''));
	            exit();
	        }
	        
	        $paytypevalue = 2;
	    }
	    
	    $data['paytype'] = $paytypevalue;
	    $data['paytime'] = date('Y-m-d H:i:s');
	    $data['payorderid'] = $payorderid;
	    $data['status'] = 2;
	    $this->OrderGoodsTable->update($data,$order['id']);
	    
	    $user_data['money'] = $user['money'] + $order['money'];
	    $get_money = $order['money'] * 0.05;
	    $get_money = sprintf("%.2f", $get_money);
	    $get_money = $get_money < 0.01 ? 0.01 : $get_money;
	    
	    $user_data['v_money'] = $user['v_money'] + $get_money;
	    $this->UserTable->update($user_data,$order['uid']);
	    $vmoney_data['uid'] = $user['id'];
	    $vmoney_data['type'] = 1;
	    $vmoney_data['money'] = $get_money;
	    $vmoney_data['remark'] = '购买激活码获得V币'.$get_money.'，'.$goods['title'];
	    $this->VmoneyTable->add($vmoney_data);
	    $vlevel_data['uid'] = $user['id'];
	    $vlevel_data['type'] = 1;
	    $vlevel_data['level'] = $get_money;
	    $vlevel_data['remark'] = '购买激活码获得V级'.$get_money.'，'.$goods['title'];
	    $this->VlevelTable->add($vlevel_data);
	    unset($vlevel_data);
	    
	    if($paytype == 'vmoney'){
	        $user_data['v_money'] = $user_data['v_money'] - $order['money'];
	        $this->UserTable->update($user_data,$order['uid']);
	        
	        $vmoney_data['uid'] = $user['id'];
	        $vmoney_data['type'] = 4;
	        $vmoney_data['money'] = (0-$order['money']);
	        $vmoney_data['remark'] = '购买激活码消耗V币'.$order['money'];
	        $this->VmoneyTable->add($vmoney_data);
	        
	    }
	    
	    $exreas = array(
	        'content'=>'购买了个人会员充值去发送',
	        'recharge'=>'个人会员充值',
	        'pushtype' => 28
	    );
	    do_jpush(array(1374,1377),'紧急消息',$exreas);
	    
	    echo json_encode(array('returncode'=>0,'message'=>'购买成功'));
	    exit();
	}
	
	//用户查看自己的激活商品
	public function rechargerecordAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','list'=>array()));
	        exit();
	    }
	    
	    $list = array();
	    $data = $this->OrderGoodsTable->listAll(array('status'=>3,'uid'=>$uid),'id DESC');
	    foreach ($data as $k=>$v){
	        $tmp_goods = $this->GoodsTable->getRow($v['goods_id']);
	        $tmp['title'] = $tmp_goods['title'];
	        $tmp['cdkey'] = $v['cdkey'];
	        $tmp['describe'] = $tmp_goods['remark'];
	        $tmp['time'] = $v['returntime'];
	        $tmp['activateurl'] = $tmp_goods['activateurl'];
	        $list[] = $tmp;
	    }
	    
	    echo json_encode(array('returncode'=>'0','list'=>$list));
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('RechargeController', 'Controller');
